package com.hxgis.statisticanalysis.common.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所属公司： 华信联创技术工程有限公司
 * 版本： 1.0
 * 创建人： 罗佳星
 * 创建时间：2017-11-08 14:23
 */
public class ResponseFormat {
    private static Map<Integer, String> msgMap = new HashMap<>();
    private static final Integer SUCCESS_CODE = 200;
    private static final Integer FAILED_CODE = 1000;

    //初始化状态码与文字说明
    static {
        msgMap.put(200, "success");
        msgMap.put(400, "Bad Request!");
        msgMap.put(401, "NotAuthorization");
        msgMap.put(405, "Method Not Allowed");
        msgMap.put(406, "Not Acceptable");
        msgMap.put(415, "不支持的媒体类型，请求数据和服务器接收的参数类型不匹配");
        msgMap.put(500, "服务器内部错误");

        msgMap.put(1001, "[服务器]运行时异常");
        msgMap.put(1002, "[服务器]空值异常");
        msgMap.put(1003, "[服务器]数据类型转换异常");
        msgMap.put(1004, "[服务器]IO异常");
        msgMap.put(1005, "[服务器]未知方法异常");
        msgMap.put(1006, "[服务器]数组越界异常");
        msgMap.put(1007, "[服务器]数组越界异常");
        msgMap.put(1008, "es数据查询错误");
    }

    public static ResponseData<Object> success() {
        return parse(200);
    }

    public static ResponseData<Object> success(Object data) {
        return parse(data, 200);
    }

    public static ResponseData<Object> error(Object data) {
        return parse(data);
    }

    public static ResponseData<Object> error(int code, Object data) {
        return parse(data, code);
    }

    public static ResponseData<String> error(String msg, Throwable t) {
        StackTraceElement firstLocalStack = t.getStackTrace()[0];
        for (StackTraceElement element : t.getStackTrace()) {
            if (element.getClassName().startsWith("com.hxgis")) {
                firstLocalStack = element;
                break;
            }
        }
        ResponseData<String> parse = parse(t.getClass().getName() + ":" + msg + "[" + firstLocalStack.getClassName() + ":" + firstLocalStack.getLineNumber() + "]", 500);
        parse.setCode(500);
        return parse;
    }

    public static ResponseData<Object> parseSuccess(String msg) {
        ResponseData<Object> res = new ResponseData<Object>();
        res.setCode(SUCCESS_CODE);
        res.setMessage(msg);
        res.setData(null);
        return res;
    }

    public static ResponseData<Object> parseError(String msg) {
        ResponseData<Object> res = new ResponseData<Object>();
        res.setCode(FAILED_CODE);
        res.setMessage(msg);
        res.setData(null);
        return res;
    }

    public static ResponseData<Object> parse(Integer code) {
        ResponseData<Object> res = new ResponseData<Object>();
        res.setCode(code);
        res.setMessage(msgMap.get(code));
        res.setData(null);
        return res;
    }

    public static <T> ResponseData<T> parse(T data) {
        ResponseData<T> res = new ResponseData<T>();
        res.setCode(SUCCESS_CODE);
        res.setMessage(msgMap.get(SUCCESS_CODE));
        res.setData(data);
        return res;
    }

    public static <T> ResponseData<List<T>> parse(List<T> data) {
        ResponseData<List<T>> res = new ResponseData<>();
        res.setCode(SUCCESS_CODE);
        res.setMessage(msgMap.get(SUCCESS_CODE));
        res.setData(data);
        return res;
    }

    public static <T> ResponseData<List<T>> parse(List<T> data, Long time) {
        ResponseData<List<T>> res = new ResponseData<>();
        res.setCode(SUCCESS_CODE);
        res.setMessage(msgMap.get(SUCCESS_CODE));
        res.setStartTime(time);
        res.setEndTime(time);
        res.setData(data);
        return res;
    }

    public static <T> ResponseData<List<T>> parse(List<T> data, Long startTime, Long endTime) {
        ResponseData<List<T>> res = new ResponseData<>();
        res.setCode(SUCCESS_CODE);
        res.setMessage(msgMap.get(SUCCESS_CODE));
        res.setStartTime(startTime);
        res.setEndTime(endTime);
        res.setData(data);
        return res;
    }

    public static <T> ResponseData<T> parse(T data, Long time) {
        ResponseData<T> res = new ResponseData<>();
        res.setCode(SUCCESS_CODE);
        res.setMessage(msgMap.get(SUCCESS_CODE));
        res.setStartTime(time);
        res.setEndTime(time);
        res.setData(data);
        return res;
    }

    public static <T> ResponseData<T> parse(T data, Long startTime, Long endTime) {
        ResponseData<T> res = new ResponseData<>();
        res.setCode(SUCCESS_CODE);
        res.setMessage(msgMap.get(SUCCESS_CODE));
        res.setStartTime(startTime);
        res.setEndTime(endTime);
        res.setData(data);
        return res;
    }

    public static <T> ResponseData<T> parse(T data, Integer code) {
        ResponseData<T> res = new ResponseData<T>();
        res.setCode(code);
        res.setMessage(msgMap.get(code));
        res.setData(data);
        return res;
    }
}
